import unit.Word;

import java.util.ArrayList;
import java.util.Map;


public class Lexer {
    private static final Lexer lexer = new Lexer();
    private final ArrayList<Word> wordList = new ArrayList<>();
    private final Map<String, String> dict = Dict.returnDict();
    private int row = 0;//行数

    private Lexer() {}

    public static Lexer getInstance() {
        return lexer;
    }

    public Map<String, String> getDict() {
        return dict;
    }

    public ArrayList<Word> getWordList() {
        return wordList;
    }

    public void lexerStart(ArrayList<String> content) {
        int sign = 0;//标记分析位置是否处于注释中
        for (String line : content) {
            row += 1;
            int i = 0;
            while (i < line.length()) {
                if (sign == 1) {
                    while (i < line.length() - 1 && !"*/".equals(line.substring(i, i + 2))) {
                        i += 1;
                    }
                    if (i < line.length() - 1 && "*/".equals(line.substring(i, i + 2))) {
                        i += 2;
                        sign = 0;
                    } else {
                        break;
                    }
                } else if (i < line.length() - 1 && "//".equals(line.substring(i, i + 2))) {
                    break;
                } else if (i < line.length() - 1 && "/*".equals(line.substring(i, i + 2))) {
                    sign = 1;
                    i += 2;
                } else if ("\"".equals(String.valueOf(line.charAt(i)))) {
                    int j = i + 1;
                    while (!"\"".equals(String.valueOf(line.charAt(j)))) {
                        j += 1;
                    }
                    addWord("STRCON", line.substring(i, j + 1));
                    i = j + 1;
                } else if (Character.isDigit(line.charAt(i))) {
                    int j = i + 1;
                    while (j < line.length() && Character.isDigit(line.charAt(j))) {
                        j += 1;
                    }
                    addWord("INTCON", line.substring(i, j));
                    i = j;
                } else if (Character.isLetter(line.charAt(i)) ||
                        "_".equals(String.valueOf(line.charAt(i)))) {
                    int j = i + 1;
                    while (j < line.length() &&
                            (Character.isDigit(line.charAt(j)) || Character.isLetter(line.charAt(j))
                                    || "_".equals(String.valueOf(line.charAt(j))))) {
                        j += 1;
                    }
                    String word = line.substring(i, j);
                    if (dict.containsKey(word)) {
                        addWord(dict.get(word), word);
                    } else {
                        addWord("IDENFR", word);
                    }
                    i = j;
                } else if (!" ".equals(String.valueOf(line.charAt(i))) &&
                        !"\t".equals(String.valueOf(line.charAt(i)))) {
                    if (i != line.length() - 1 && dict.containsKey(line.substring(i, i + 2))) {
                        addWord(dict.get(line.substring(i, i + 2)), line.substring(i, i + 2));
                        i += 2;
                    } else {
                        addWord(dict.get(line.substring(i, i + 1)), line.substring(i, i + 1));
                        i += 1;
                    }
                } else {
                    i += 1;
                }
            }
        }
    }

    public void addWord(String type, String name) {
        wordList.add(new Word(row, type, name));
    }
}
